package com.hjm.framework.task;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.hjm.common.utils.HttpUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.request.AutoLoadData;
import com.hjm.framework.cache.RedisUtils;
import com.hjm.project.formalPos.formalPos.domain.FormalPos;
import com.hjm.project.formalPos.formalPos.service.IFormalPosService;
import com.hjm.project.merchantInfo.merchantInfo.domain.MerchantInfo;
import com.hjm.project.merchantInfo.merchantInfo.service.IMerchantInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 定事任务
 */
@Configuration
@EnableScheduling
@Slf4j
public class StaticScheduleTask {

    @Resource
    private IMerchantInfoService merchantInfoService;
    @Resource
    private IFormalPosService formalPosService;


    /**
     * 5分钟执行一次
     */
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void initConfigTask(){
        AtomicInteger counter = new AtomicInteger(0);

        RedisUtils redis = RedisUtils.getInstance();
        String merKey = "merchant:cq:";
        MerchantInfo merchantInfo = new MerchantInfo();
        merchantInfo.setMerTb(2);
        List<Map> merchantInfos = merchantInfoService.selectMerchantInfoList1(merchantInfo);
        log.info("开始同步扫描--------------------------------->");
        log.info("商户数量---------------------------------> {}",merchantInfos.size() + "个");
        for (Map info : merchantInfos) {

            String merno = info.get("LD_MERNO").toString();
            merKey += merno;
            Object obj = redis.getString(merKey);
            FormalPos fp = new FormalPos();
            fp.setMerno(merno);
            List<FormalPos> formalPosList = formalPosService.selectFormalPosList(fp);
            if (ObjectUtil.isEmpty(obj) || StringUtils.isBlank(info.get("LEGAL_CERNO").toString()) || CollectionUtils.isEmpty(formalPosList)) {
                JSONObject data = new JSONObject();
                data.put("merNo", merno);
                String send = null;
                try {
                    send = HttpUtils.send(AutoLoadData.sendLdMerNo, data, "UTF-8");
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (StringUtils.isBlank(send)) {
                    continue;
                }

                if (!"200".equals(JSONObject.parseObject(send).getString("statusCode"))) {
                    continue;
                }

                JSONObject dataResult = JSONObject.parseObject(send);
                JSONObject result = dataResult.getJSONObject("data");
                Map<String, String> dataSync = merchantInfoService.syncMerInfoJob(result);
                if ("200".equals(dataSync.get("code"))){
                    redis.addSet(merKey,merno);
                    counter.incrementAndGet();
                    merchantInfoService.updateMerchantSync(merno);
                } else {
                    redis.delKey(merKey);
                    log.info("获取商户信息---------------------------------> {}",send);
                    log.info("同步商户失败,{}---------------------------------> {}",merno,dataSync);
                }

            }
            log.info("同步成功商户---------------------------------> {}",counter.get() + "个");
        }
        log.info("总商户---------------------------------> {}",merchantInfos.size() + "个");
        log.info("同步成功商户---------------------------------> {}",counter.get() + "个");
        log.info("同步失败商户---------------------------------> {}",merchantInfos.size() - counter.get() + "个");
        log.info("同步扫描结束--------------------------------->");
    }

}

